<template>
  <section class="f-goods-order">
    <div class="w-card my-content-box" v-if="!isDetail">
      <div class="title-box">
        <span class="title-text">商品订单列表</span>
      </div>

      <div class="my-search-box">
        <div>
          <span>订单ID：</span>
          <el-input v-model="searchForm.sysOrderId" clearable placeholder="请输入订单ID" />
        </div>
        <div>
          <span>联系人号码：</span>
          <el-input v-model="searchForm.receivePhone" clearable placeholder="请输入联系人号码" />
        </div>
        <div>
          <span>订单状态：</span>
          <el-select v-model="searchForm.orderStatus" clearable>
            <el-option v-for="item in orderStatusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </div>

      <div class="my-search-box">
        <div>
          <span>代理商：</span>
          <el-input v-model="searchForm.shopName" clearable placeholder="请输入代理商名称" />
        </div>
        <div class="datebox">
          <span>创建时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始时间"
            end-placeholder="结束时间" @change="(e) => {console.log(e)}" />
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :max-height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="selectionChange">
          <el-table-column type="index" width="55" align="center" />
          <!-- <el-table-column type="selection" width="55" /> -->
          <el-table-column prop="sysOrderId" label="订单ID" align="center">
            <template v-slot="scope">
              <el-link style="font-size:12px" type="primary"
                @click="pageJump('/orderMng/goodsOrder/detail',scope.row.sysOrderId)">
                {{ scope.row.sysOrderId || '' }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="代理商" align="center">
            <template v-slot="scope">
              <p>{{ scope.row.shopName || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="orderStatus" label="订单状态" align="center">
            <template v-slot="scope">
              <div style="color: rgb(182, 152, 63);">
                {{ orderStatus[scope.row.orderStatus] || '' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="goodsName" label="订单商品" align="center" min-width="100">
            <template v-slot="scope">
              <div class="slh-text">{{ scope.row.goodsName || '' }}（{{ scope.row.baseNum }}台）</div>
              <div>
                商品价格：
                <span style="color: red">￥{{ scope.row.actualPrice }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="goodsName" label="下单数量" align="center" width="80">
            <template v-slot="scope">
              <p>{{ scope.row.goodsNum }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="payMoney" label="支付信息" align="center">
            <template v-slot="scope">
              <div>
                购买方式：
                <span>{{ scope.row.buyType == 1? '金额':  scope.row.buyType == 2? '积分': '--'}}</span>
              </div>
              <div v-if="scope.row.buyType == 1">
                总金额：
                <span style="color: red">￥{{ scope.row.payMoney || '' }}</span>
              </div>
              <div v-if="scope.row.buyType == 2">
                总积分：
                <span style="color: red">￥{{ scope.row.payPoints || '' }}</span>
              </div>
              <div>
                支付时间：
                <span>{{ scope.row.payTime ? getDateTime(scope.row.payTime)[6] : '--' }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="receiveName" label="物流信息" align="center" min-width="200">
            <template v-slot="scope">
              <div class="receiveName-box">
                <div>
                  <span>收件人：{{ scope.row.isLook ? Base64.decode(scope.row.receiveNameEncrypt || '') : scope.row.receiveName }}</span>
                </div>
                <div>
                  <span>联系电话：{{ scope.row.isLook ? Base64.decode(scope.row.receivePhoneEncrypt || '') : scope.row.receivePhone }}</span>
                </div>
                <div>
                  <span>收货地址：{{ (scope.row.provinceAreaName || '') + (scope.row.cityAreaName || '') + (scope.row.areaAreaName || '') }}{{ scope.row.isLook ? Base64.decode(scope.row.receiveAddressDetailEncrypt || '') : scope.row.receiveAddressDetail }}</span>
                </div>
                <div>
                  <span>物流公司：{{ scope.row.logisticsName || '' }}</span>
                </div>
                <div>
                  <span>物流单号：{{ scope.row.logisticsNum || '' }}</span>
                </div>
                <div v-show="scope.row.logisticsStartTime">
                  <span>发货时间：{{ scope.row.logisticsStartTime ? getDateTime(scope.row.logisticsStartTime)[6] : '' }}</span>
                </div>
                <div v-show="scope.row.logisticsReceiveTime">
                  <span>收货时间：{{ scope.row.logisticsReceiveTime ? getDateTime(scope.row.logisticsReceiveTime)[6] : '' }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <!-- <div class="footer_btn_box">
              <el-button :disabled="chooseList.length == 0" @click="settingHandle('delete')" type="danger" plain>删除</el-button>
            </div> -->
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <router-view></router-view>
  </section>
</template>
  
  <script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import api from '../api'

export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const uploadRef = ref(null)

    const state = reactive({
      isDetail: false,

      searchForm: {
        "endTime": "",
        "orderBy": "id desc",
        "orderStatus": "", // 订单状态，5-待支付，6-线下支付待审核，10-已支付，30-已发货，40-已收货，50-订单关闭
        "receivePhone": "",
        "startTime": "",
        "sysOrderId": "",
        shopName: ''
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      chooseList: [],
      selectData: {},

      orderStatusList: [
        { value: '5', label: '待支付' },
        { value: '6', label: '线下支付待审核' },
        { value: '10', label: '已支付' },
        { value: '30', label: '已发货' },
        { value: '40', label: '已收货' },
        { value: '50', label: '订单关闭' },
      ],
      orderStatus: {
        '5': '待支付',
        '6': '线下支付待审核',
        '10': '已支付',
        '30': '已发货',
        '40': '已收货',
        '50': '订单关闭',
      }
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      state.isDetail = (route.path != '/orderMng/goodsOrder')
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "endTime": "",
        "orderBy": "id desc",
        "orderStatus": "",
        "receivePhone": "",
        "startTime": "",
        "sysOrderId": "",
        shopName: ''
      }
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: state.searchForm.date && state.searchForm.date[0],
        endTime: state.searchForm.date && state.searchForm.date[1],
      }
      api.asdOrderList(obj).then(res => {
        state.tableData = res.data.list
        state.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function pageJump(path, sysOrderId) {
      router.push({
        path,
        query: {
          sysOrderId
        }
      })
    }

    function settingHandle(type, data) {
      let title = ""
      let msg = ''
      if (type == 'shelveStatus') {
        title = '设备上下架提示'
        msg = `您确定要将所选的设备${data == 1 ? '下架' : '上架'}吗？`
      } else if (type == 'delete') {
        title = '设备删除提示'
        msg = `您确定要将所选的设备删除吗？`
      }
      // if (state.chooseList.length == 0) {
      //   return
      // }
      state.selectData = data
      //   confirmPasswordRef.value.dialogShow(title, msg, type)
    }

    function selectionChange(list) {
      state.chooseList = list
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 320
      setIsDetail()
    })

    return {
      ...toRefs(state),
      uploadRef,
      getDateTime,
      searchHandle,
      resetHandle,
      handleCurrentChange,
      handleSizeChange,
      settingHandle,
      selectionChange,
      pageJump,
    }
  }
}
  </script>
  
  <style lang="scss" scoped>
.f-goods-order {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    > .title-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 350px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 120px;
          text-align: right;
        }
      }

      .datebox {
        width: 450px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
  .oStatus {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    &::before {
      content: "";
      display: block;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      flex-shrink: 0;
    }
  }
  .slh-text {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price-text {
    color: red;
  }
  .receiveName-box {
    display: flex;
    flex-flow: column;
    align-items: center;
  }
}
</style>